package com.bibrApp.difu.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.utils.ToastUtils;
import com.bibrApp.difu.utils.UShape;

import androidx.appcompat.app.AppCompatDialog;


/**
 * Created by dj
 * on 2020/2/28.
 */

public class CustomDialog extends AppCompatDialog {

    public static final int CONTENT_TEXT_TYPE = 0;//default,文本框
    public static final int CONTENT_EDIT_TYPE = 1;//可选择,输入框

    public static class Builder {
        private CustomDialog mDialog;
        private View mRootView;
        private Context mContext;

        @SuppressLint("InflateParams")
        public Builder(Context context) {
            mContext = context;
            mDialog = new CustomDialog(context, R.style.OrderDialogStyle);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mRootView = inflater.inflate(R.layout.dialog_custom, null, false);
            UShape.setBackgroundDrawable(UShape.getCornerDrawable(UShape.getColor(R.color.white,context), 8), mRootView);
            mDialog.addContentView(mRootView, new ViewGroup.LayoutParams((int) (ScreenUtils.getScreenDispaly(context)[0] * 0.75),
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            mDialog.setCancelable(false);
            mDialog.setCanceledOnTouchOutside(false);

            mDialog.setOnKeyListener((dialog, keyCode, event) -> {
                if (KeyEvent.KEYCODE_BACK == keyCode && event.getAction() == KeyEvent.ACTION_DOWN) {
                    dialog.dismiss();
                    return true;
                }
                return false;
            });
        }

        public Builder setTitle(String title) {
            if (TextUtils.isEmpty(title)) {
                return this;
            }
            TextView textView = mRootView.findViewById(R.id.tv_dialog_title);
            textView.setVisibility(View.VISIBLE);
            textView.setText(title);
            return this;
        }

        public Builder setMessage(String message, int type) {
            switch (type) {
                case CONTENT_TEXT_TYPE:
                    TextView textView = mRootView.findViewById(R.id.tv_dialog_content);
                    textView.setVisibility(View.VISIBLE);
                    textView.setText(message);
                    break;
                case CONTENT_EDIT_TYPE:
                    EditText editText = mRootView.findViewById(R.id.et_dialog_content);
                    editText.setVisibility(View.VISIBLE);
                    editText.setHint(message);
                    break;
            }
            return this;
        }

        public Builder addPositiveButton(String title, final View.OnClickListener listener) {
            LinearLayout layout = mRootView.findViewById(R.id.dialog_footer);
            layout.setVisibility(View.VISIBLE);
            TextView btn_ok = mRootView.findViewById(R.id.btn_dialog_ok);
            btn_ok.setVisibility(View.VISIBLE);
            btn_ok.setText(title);
            btn_ok.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onClick(v);
                }
                dismiss();
            });
            return this;
        }

        public Builder addPositiveButton(String title, final OnEditClickListener listener) {

            LinearLayout layout = mRootView.findViewById(R.id.dialog_footer);
            layout.setVisibility(View.VISIBLE);
            TextView btn_ok = mRootView.findViewById(R.id.btn_dialog_ok);
            btn_ok.setVisibility(View.VISIBLE);
            btn_ok.setText(title);
            btn_ok.setOnClickListener(v -> {
                if (listener != null) {
                    EditText editText = mRootView.findViewById(R.id.et_dialog_content);
                    String text = editText.getText().toString().replaceAll(" ","");
                    if(TextUtils.isEmpty(text) || text.length() < 6){
                        ToastUtils.ShowToast(mContext ,"输入内容有误");
                        return;
                    }
                    listener.onPositiveClick(v,text);
                    dismiss();
                }
            });
            return this;
        }

        public Builder addCancelButton(String title, View.OnClickListener listener) {

            LinearLayout layout = mRootView.findViewById(R.id.dialog_footer);
            layout.setVisibility(View.VISIBLE);
            TextView btn_cancel = mRootView.findViewById(R.id.btn_dialog_cancel);
            btn_cancel.setVisibility(View.VISIBLE);
            btn_cancel.setText(title);
            btn_cancel.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onClick(v);
                }
                dismiss();
            });
            return this;
        }

        public void show() {
            if (mDialog != null && !mDialog.isShowing()) {
                mDialog.show();
            }
        }

        public void dismiss() {
            if (mDialog != null && mDialog.isShowing()) {
                mDialog.dismiss();
            }
        }


    }

    private CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public interface OnEditClickListener {
        void onPositiveClick(View view, String text);
    }
}
